package com.netmi.myvideopicker.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.netmi.myvideopicker.BR;
import com.netmi.myvideopicker.R;
import com.netmi.myvideopicker.bean.VideoItem;
import com.netmi.myvideopicker.data.VideoPicker;
import com.netmi.myvideopicker.databinding.AdapterImageListItemBinding;
import com.netmi.myvideopicker.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 加载相册图片的RecyclerView适配器
 * <p>
 * 用于替换原项目的GridView，使用局部刷新解决选中照片出现闪动问题
 * <p>
 * 替换为RecyclerView后只是不再会导致全局刷新，
 * <p>
 * 但还是会出现明显的重新加载图片，可能是picasso图片加载框架的问题
 * <p>
 * Author: nanchen
 * Email: liushilin520@foxmail.com
 * Date: 2017-04-05  10:04
 */

public class VideoRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private static final int ITEM_TYPE_CAMERA = 0;  //第一个条目是相机
    private static final int ITEM_TYPE_NORMAL = 1;  //第一个条目不是相机
    private VideoPicker videoPicker;
    private Activity mActivity;
    private List<VideoItem> videos = new ArrayList<>();       //当前需要显示的所有的图片数据
    private ArrayList<VideoItem> mSelectedVideos; //全局保存的已经选中的图片数据
    private boolean isShowCamera;         //是否显示拍照按钮
    private int mImageSize;               //每个条目的大小
    private LayoutInflater mInflater;
    private OnImageItemClickListener listener;   //图片被点击的监听

    public void setOnImageItemClickListener(OnImageItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnImageItemClickListener {
        void onImageItemClick(View view, VideoItem imageItem, int position);
    }

    public void refreshData(List<VideoItem> videos) {
        if (videos.size() > 0) {
            this.videos.clear();
            if (videos != null)
                this.videos.addAll(videos);
        }
        notifyDataSetChanged();
    }


    public VideoRecyclerAdapter(Activity mActivity,List<VideoItem> videos,OnImageItemClickListener listener) {
        this.mActivity = mActivity;
        this.videos = videos;
        this.listener = listener;
        mImageSize = Utils.getImageItemWidth(mActivity);
    }

    /**
     * 构造方法
     */


//    public VideoRecyclerAdapter(Activity activity, ArrayList<ImageItem> images) {
//        this.mActivity = activity;
//        if (images == null || images.size() == 0) this.images = new ArrayList<>();
//        else this.images = images;
//
//        mImageSize = Utils.getImageItemWidth(mActivity);
//        videoPicker = ImagePicker.getInstance();
//        isShowCamera = videoPicker.isShowCamera();
//        mSelectedImages = videoPicker.getSelectedImages();
//        mInflater = LayoutInflater.from(activity);
//    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        if (viewType == ITEM_TYPE_CAMERA) {
//            return new CameraViewHolder(mInflater.inflate(R.layout.adapter_camera_item, parent, false));
//        }
//        return new ImageViewHolder(mInflater.inflate(R.layout.adapter_image_list_item, parent, false));

        ViewDataBinding viewDataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.adapter_image_list_item, parent, false);
        return new CameraViewHolder(viewDataBinding);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof CameraViewHolder) {
            CameraViewHolder cameraViewHolder = (CameraViewHolder) holder;
            AdapterImageListItemBinding binding = (AdapterImageListItemBinding) cameraViewHolder.getBinding();
//            BindUtils.ivGlideLoadNative(binding.ivThumb, videos.get(position).path);
            binding.setVariable(BR.item, videos.get(position));
            binding.executePendingBindings();

            binding.checkView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null){
                        listener.onImageItemClick(view,videos.get(position),position);
                    }
                }
            });
            binding.ivThumb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null){
                        listener.onImageItemClick(view,videos.get(position),position);
                    }
                }
            });

        }
//        else if (holder instanceof ImageViewHolder) {
//            ((ImageViewHolder) holder).bind(position);
//        }


    }

    @Override
    public int getItemViewType(int position) {
        if (isShowCamera) return position == 0 ? ITEM_TYPE_CAMERA : ITEM_TYPE_NORMAL;
        return ITEM_TYPE_NORMAL;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return videos == null ? 0 : videos.size();
    }

    public VideoItem getItem(int position) {
        if (isShowCamera) {
            if (position == 0) return null;
            return videos.get(position - 1);
        } else {
            return videos.get(position);
        }
    }

    private class ImageViewHolder extends RecyclerView.ViewHolder {

        View rootView;
        ImageView ivThumb;
        View mask;
        View checkView;
//        SuperCheckBox cbCheck;


        ImageViewHolder(View itemView) {
            super(itemView);
//            rootView = itemView;
//            ivThumb = (ImageView) itemView.findViewById(R.id.iv_thumb);
//            mask = itemView.findViewById(R.id.mask);
//            checkView=itemView.findViewById(R.id.checkView);
//            cbCheck = (SuperCheckBox) itemView.findViewById(R.id.cb_check);
//            itemView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mImageSize)); //让图片是个正方形
        }

//        void bind(final int position){
//            final ImageItem imageItem = getItem(position);
//            ivThumb.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (listener != null) listener.onImageItemClick(rootView, imageItem, position);
//                }
//            });
//            checkView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    cbCheck.setChecked(!cbCheck.isChecked());
//                    int selectLimit = imagePicker.getSelectLimit();
//                    if (cbCheck.isChecked() && mSelectedImages.size() >= selectLimit) {
//                        Toast.makeText(mActivity.getApplicationContext(), mActivity.getString(R.string.ip_select_limit, selectLimit), Toast.LENGTH_SHORT).show();
//                        cbCheck.setChecked(false);
//                        mask.setVisibility(View.GONE);
//                    } else {
//                        imagePicker.addSelectedImageItem(position, imageItem, cbCheck.isChecked());
//                        mask.setVisibility(View.VISIBLE);
//                    }
//                }
//            });
//            //根据是否多选，显示或隐藏checkbox
//            if (imagePicker.isMultiMode()) {
//                cbCheck.setVisibility(View.VISIBLE);
//                boolean checked = mSelectedImages.contains(imageItem);
//                if (checked) {
//                    mask.setVisibility(View.VISIBLE);
//                    cbCheck.setChecked(true);
//                } else {
//                    mask.setVisibility(View.GONE);
//                    cbCheck.setChecked(false);
//                }
//            } else {
//                cbCheck.setVisibility(View.GONE);
//            }
//            imagePicker.getImageLoader().displayImage(mActivity, imageItem.path, ivThumb, mImageSize, mImageSize); //显示图片
//        }

    }

    private class CameraViewHolder extends RecyclerView.ViewHolder {

        private ViewDataBinding binding;
        private int position ;

        CameraViewHolder(ViewDataBinding binding) {
            super(binding.getRoot());
            binding.getRoot().setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mImageSize)); //让图片是个正方形
            this.binding = binding;
        }

        public void setBinding(ViewDataBinding binding) {
            this.binding = binding;
        }

        public ViewDataBinding getBinding() {
            return this.binding;
        }

//        void bindCamera(){
//            mItemView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mImageSize)); //让图片是个正方形
//            mItemView.setTag(null);
//            mItemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (!((ImageBaseActivity) mActivity).checkPermission(Manifest.permission.CAMERA)) {
//                        ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.CAMERA}, ImageGridActivity.REQUEST_PERMISSION_CAMERA);
//                    } else {
//                        imagePicker.takePicture(mActivity, ImagePicker.REQUEST_CODE_TAKE);
//                    }
//                }
//            });
//        }
    }
}
